﻿@model Tuple<TracNghiem.Areas.Admin.Models.Test_QuestionModel,List<TracNghiem.Areas.Admin.Models.Test_AnswerModel>>
<div class="container_12">       
    <div class="grid_12">            
        <div class="module">
                <h2 align="right"><span><a href="@Url.Action("Index", "Test_Question", new { id=ViewBag.dichvu_id})">Quay lại</a></span></h2>                       
                <div class="module-body">
                @using (Html.BeginForm("EditTest_Question", "Test_Question", FormMethod.Post, new { id = "Test_Question", name = "Test_Question" }))
                {                      
                    <div align="center"><h3>SỬA CÂU HỎI TRẮC NGHIỆM</h3>
                    <div align="center"><h3>Tên dịch vụ: <font color="maroon">@ViewBag.ten_dich_vu</font></h3></div> 
                        @*<div class="account" align="center"> 
                            <img src="@Url.Content("~/Content/themes/template/image//Body_Images/Add_User.png")" width="12%" />
                        </div>*@    
                    </div>
                    if (TempData["error"] != null)
                    {                            
                        <span class="notification n-error">@TempData["error"]</span>
                    } 
                    if (TempData["msg"] != null)
                    {                            
                        <span class="notification n-success">@TempData["msg"]</span>
                    }
                    <input type="hidden" name="dichvu_id" value="@ViewBag.dichvu_id" />
                    <input type="hidden" name="cb_index" id="cb_index" value="" />
                    <table> 
                        <div style="width:50%; padding-left: 5%; float:left"> 
                            <p>
                                <div>
                                    <label>Câu hỏi:</label>
                                </div>
                                <div>                                   
                                    @Html.TextAreaFor(model => model.Item1.name, new { rows =2 , @class = "input-long" })
                                    <font color="red">(*)</font>
                                    <label style="width:56%; text-align:left;color:red">
                                        @Html.ValidationMessageFor(model => model.Item1.name)
                                    </label>
                                </div>                                                
                            </p> 
                            <p>
                                <div>
                                    Phân loại:
                                    @if (ViewBag.option_id != null)
                                    {                               
                                        @Html.DropDownList("phanloai_id", (IEnumerable<SelectListItem>)ViewBag.option_id, new { style = "margin-left:13px" })          
                                        @*@Html.DropDownListFor(model => model.Item1.phanloai_id, Model.Item1.option_id, new { })*@
                                    }
                                </div>                          
                            </p>  
                            <p>
                                <div>
                                    <label>Ghi chú:</label>
                                </div>
                                <div>
                                   @Html.TextAreaFor(model => model.Item1.ghi_chu, new { rows = 3, @class = "input-long" })
                                </div>
                            </p>
                            <p> 
                                <div>
                                Trạng thái:                                
                                <select name="status">
                                    <option value="1" @(Model.Item1.status == "Bình thường" ? "selected=selected" : "")>Bình thường</option>
                                    <option value="0" @(Model.Item1.status == "Khóa" ? "selected=selected" : "")>Khóa</option>                            
                                </select>                                        
                                </div>    
                            </p>                            
                            <p> 
                                <div>
                                Ngày kết thúc:                                
                                    @Html.TextBoxFor(model => model.Item1.ngay_ket_thuc, new { id = "date" })                                    
                                </div>
                            </p>
                            @Html.HiddenFor(model => model.Item1.id)
                            <input type="hidden" name="cauhoi_id" value="@ViewBag.cau_hoi_id">
                        </div>
                        <br>
                        <div id="cau_tl" style="width:45%; float:right">                            
                            <div>
                                <a href="#" onclick="AddTextBox();">Thêm câu trả lời</a>
                            </div>
                            @{int stt = 0;}
                            @foreach (var item in Model.Item2)
                            {
                                stt++;
                                ViewBag.count = stt;
                                <div>
                                    @if (stt <= 2)
                                    {
                                        @: Câu trả lời @stt:
                                    }
                                    else
                                    { 
                                        @: Câu trả lời @stt:(<a href="#" onclick="RemoveTextBox(this)">Xóa</a>)
                                    }
                                    <br>
                                    <textarea rows="2" class="input-long" name="cau_tra_loi">@item.name</textarea>
                                    <input type="hidden" name="cau_tl_hidden" value="@item.id" />
                                    <input type="checkbox" value="1" @(item.status == "Bình thường" ? "checked=checked" : "") name='cb' onclick="onlyMe('Test_Question',this);" />
                                </div>
                            }
                        </div>    
                        
                    </table>
                    <div class="button" align="center">
                        <p style="width:26%; text-align:center">
                            <input class="submit-green" type="button" onclick="check_submit()" value="Sửa"/> 
                            <input class="submit-gray" type="reset" value="Hủy" />                            
                        </p>
                    </div>
                }
                </div> <!-- End .module-body -->
        </div>  <!-- End .module -->
        <div style="clear:both;"></div>
    </div> <!-- End .grid_12 --> 
    <div style="clear:both;"></div>
</div> <!-- End .container_12 -->
<script type="text/javascript">
    $(document).ready(function () {
        $("#date").datepicker({ dateFormat: 'dd/mm/yy' });
    });
</script>

<script type="text/javascript">
    var x =  @ViewBag.count;    
    function GetDynamicTextBox2(value) {
        x += 1;
        return 'Câu trả lời ' + x + ':(<a href="#" onclick="RemoveTextBox(this)">Xóa</a>)'
        + '<br><textarea rows="1" class="input-long" name="cau_tra_loi"></textarea>&nbsp;<input type="checkbox" value="1" name="cb" onclick="onlyMe(Test_Question,this);" />';
    }
    function AddTextBox() {
        var div2 = document.createElement('DIV');
        div2.innerHTML = GetDynamicTextBox2("");
        document.getElementById("cau_tl").appendChild(div2);
    }
    function RemoveTextBox(div) {
        document.getElementById("cau_tl").removeChild(div.parentNode);
    }
    var status = false;
    function check_submit() {
        var hiddencontrol = document.getElementById("cb_index");
        for (i = 0; i < document.forms[0].cau_tra_loi.length; i++) {
            if (document.forms[0].cau_tra_loi[i].value == "") {
                alert("Cần điền đầy đủ câu trả lời");
                document.forms[0].cau_tra_loi[i].focus();
                return false;
            }
        }
        var checks = document.getElementsByName('cb');
        var boxLength = checks.length;
        for (i = 0; i < boxLength; i++) {
            if (checks[i].checked) {
                status = true;
                hiddencontrol.value = i;

            }
        }
        if (status == false) {
            alert("Cần chọn câu trả lời đúng");
            return false;
        }
        document.Test_Question.submit();
    }

    function unCheckThem(Formname) {
        var checks = document.getElementsByName('cb');
        var boxLength = checks.length;
        for (i = 0; i < boxLength; i++) {
            checks[i].checked = false;
        }
        return;
    }
    function onlyMe(FormName, myBoxId) {
        unCheckThem(FormName);
        myBoxId.checked = "true";
    }
</script>